﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;


using ConstructionBidOnline.DAL;
using ConstructionBidOnline.DAL.dsUserFeedbackTableAdapters;
using ConstructionBidOnline.Common;
namespace ConstructionBidOnline.Biz
{
   public class UserFeedback:BOBase
    {
       #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
       protected UserFeedback() { }

        #endregion

       #region Public Properties

       #region Instance Properties

       /// <summary>
       /// PKID of the UserFeedback.
       /// </summary>
       public int UserFeedbackId
       {
           get { return _userfeedbackId; }
           set
           {
               if (_userfeedbackId != value)
               {
                   if (OnPropertyChanging("UserFeedbackId", _userfeedbackId, value))
                   {
                       int oldValue = _userfeedbackId;
                       _userfeedbackId = value;

                       OnPropertyChanged("UserFeedbackId", oldValue, value);
                   }
               }
           }
       }  private int _userfeedbackId = -1;

       /// <summary>
       /// FirstName.
       /// </summary>
       public string FirstName
       {
           get { return _firstname; }
           set
           {
               if (_firstname != value)
               {
                   if (OnPropertyChanging("FirstName", _firstname, value))
                   {
                       string oldValue = _firstname;
                       _firstname = value;
                       OnPropertyChanged("FirstName", oldValue, value);
                   }
               }
           }
       }  private string _firstname = null;


       /// <summary>
       /// LastName.
       /// </summary>
       public string LastName
       {
           get { return _lastname; }
           set
           {
               if (_lastname != value)
               {
                   if (OnPropertyChanging("LastName", _lastname, value))
                   {
                       string oldValue = _lastname;
                       _lastname = value;
                       OnPropertyChanged("LastName", oldValue, value);
                   }
               }
           }
       }  private string _lastname = null;




       /// <summary>
       /// EmailAddress.
       /// </summary>
       public string EmailAddress
       {
           get { return _emailaddress; }
           set
           {
               if (_emailaddress != value)
               {
                   if (OnPropertyChanging("EmailAddress", _emailaddress, value))
                   {
                       string oldValue = _emailaddress;
                       _emailaddress = value;
                       OnPropertyChanged("EmailAddress", oldValue, value);
                   }
               }
           }
       }  private string _emailaddress = null;

       /// <summary>
       /// Phone 
       /// </summary>
       public string Phone
       {
           get { return _phone; }
           set
           {
               if (_phone != value)
               {
                   if (OnPropertyChanging("Phone", _phone, value))
                   {
                       string oldValue = _phone;
                       _phone = value;
                       OnPropertyChanged("Phone", oldValue, value);
                   }
               }
           }
       }  private string _phone = null;




       /// <summary>
       /// User Message
       /// </summary>
       public string Messages
       {
           get { return _messages; }
           set
           {
               if (_messages != value)
               {
                   if (OnPropertyChanging("Messages", _messages, value))
                   {
                       string oldValue = _messages;
                       _messages = value;
                       OnPropertyChanged("Messages", oldValue, value);
                   }
               }
           }
       }  private string _messages = null;



       /// <summary>
       /// Date/time at which the User Feedback was Posted.
       /// </summary>
       public DateTime? PostedDate
       {
           get { return _posteddate; }
           set
           {
               if (_posteddate != value)
               {
                   if (OnPropertyChanging("PostedDate", _posteddate, value))
                   {
                       DateTime? oldValue = _posteddate;
                       _posteddate = value;
                       OnPropertyChanged("PostedDate", oldValue, value);
                   }
               }
           }
       }  private DateTime? _posteddate = null;

       


       

       #endregion

       #endregion

       #region Public Methods

       #region CreateNew

       /// <summary>
       /// Creates a new, empty UserFeedback object.
       /// </summary>
       public static UserFeedback CreateNew()
       {
           UserFeedback obj = new UserFeedback();

           // Switch state to Added prior to initializing property values.
           obj.ObjState = ObjectState.Added;
           obj.PostedDate = DateTime.Now;
           //obj.ActionDate = DateTime.Now;
           return (obj);
       }


       [MethodImpl(MethodImplOptions.Synchronized)]
       public static UserFeedback CreateNew(UserFeedbackCollection collection)
       {
           UserFeedback obj = CreateNew();

           // Get next/unique ID for the new object.
           int minVal = 0;
           foreach (UserFeedback userfeedback in collection)
           {
               if (userfeedback.UserFeedbackId < minVal)
                   minVal = userfeedback.UserFeedbackId;
           }
           obj.UserFeedbackId = --minVal;
           collection.Add(obj);

           return (obj);
       }

       #endregion

       #region GetCompleteUserFeedback

       /// <summary>
       /// Returns a collection of UserFeedback.
       /// </summary>
       public static UserFeedbackCollection GetCompleteUserFeedback()
       {
          UserFeedBackTableAdapter adapter = new UserFeedBackTableAdapter();
           adapter.Connection = AppSettings.GetDbConn();

           dsUserFeedback.UserFeedBackDataTable tbl = adapter.GetData(null);

           UserFeedbackCollection list = new UserFeedbackCollection();

           if (tbl.Rows.Count > 0)
           {
               foreach (dsUserFeedback.UserFeedBackRow row in tbl.Rows)
               {
                   UserFeedback directory = new UserFeedback();
                   RelationalToObject(directory, row);
                   list.Add(directory);
               }
           }

           return (list);
       }

       #endregion

       #region GetUserFeedbackByID

       /// <summary>
       /// Returns an UserFeedback object for the specified 'UserFeedbackID' or null if no UserFeedback with the specified parameter(s) exists.
       /// </summary>
       public static UserFeedback GetUserFeedbackByID(int userfeedbackid)
       {
          UserFeedBackTableAdapter adapter = new UserFeedBackTableAdapter();
           adapter.Connection = AppSettings.GetDbConn();
           dsUserFeedback.UserFeedBackDataTable tbl = adapter.GetData(userfeedbackid);

           if (tbl.Rows.Count == 1)
           {
               UserFeedback directoryObj = new UserFeedback();
               RelationalToObject(directoryObj, tbl.Rows[0]);
               return (directoryObj);
           }
           else if (tbl.Rows.Count > 1)
               throw new ApplicationException("Database integrity error: Specified 'userLogin' returned more than one User record.");

           return (null);
       }

       #endregion

       #region GetUserFeedbackByFirstName

       ///// <summary>
       ///// Returns an UserFeedback List object for the specified 'firstname' or null if no name with the specified parameter(s) exists.
       ///// </summary>
       //public static UserFeedback GetUserFeedbackByFirstName(string firstname)
       //{
       //    UserFeedBackTableAdapter adapter = new UserFeedBackTableAdapter();
       //    adapter.Connection = AppSettings.GetDbConn();
       //    dsUserFeedback.UserFeedBackDataTable tbl = adapter.GetDataByFirstName(firstname);

       //    if (tbl.Rows.Count == 1)
       //    {
       //        UserFeedback user = new UserFeedback();
       //        RelationalToObject(user, tbl.Rows[0]);
       //        return (user);
       //    }
       //    else if (tbl.Rows.Count > 1)
       //        throw new ApplicationException("Database integrity error: Specified 'userLogin' returned more than one User record.");

       //    return (null);
       //}

       #endregion

       #region GetUserFeedbackByEmailAddres

       ///// <summary>
       ///// Returns an UserFeedback object for the specified 'emailaddres' or null if no name with the specified parameter(s) exists.
       ///// </summary>
       //public static Directory GetUserFeedbackByEmailAddres(string emailaddres)
       //{
       //    UserFeedBackTableAdapter adapter = new UserFeedBackTableAdapter();
       //    adapter.Connection = AppSettings.GetDbConn();
       //    dsUserFeedback.UserFeedBackDataTable tbl = adapter.GetDataByEmailAddress(emailaddres);

       //    if (tbl.Rows.Count == 1)
       //    {
       //        UserFeedback user = new UserFeedback();
       //        RelationalToObject(user, tbl.Rows[0]);
       //        return (user);
       //    }
       //    else if (tbl.Rows.Count > 1)
       //        throw new ApplicationException("Database integrity error: Specified 'userLogin' returned more than one User record.");

       //    return (null);
       //}

       #endregion

       #region Update

       /// <summary>
       /// Updates the specified UserFeedback.
       /// </summary>
       public static void Update(UserFeedback obj)
       {
           bool updateUserFeedback = false;

           // Do not update invalid objects.
           if (!obj.Validate())
           {
               StringBuilder sb = new StringBuilder(2048);
               sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
               foreach (ValidationError err in obj.Errors)
                   sb.Append("\n* " + err.Element + ": " + err.Message);

               throw new InvalidObjectException(sb.ToString());
           }
           else
           {
               updateUserFeedback = (obj.ObjState != ObjectState.Unchanged);

               if (!updateUserFeedback)
                   return;
           }

           ObjectState originalObjectState = obj.ObjState;

           dsUserFeedback.UserFeedBackDataTable tbl = null;
           if (updateUserFeedback)
           {

               UserFeedBackTableAdapter adapter = new UserFeedBackTableAdapter();
               adapter.Connection = AppSettings.GetDbConn();


               tbl = new dsUserFeedback.UserFeedBackDataTable();
               dsUserFeedback.UserFeedBackRow row = tbl.NewUserFeedBackRow();
               ObjectToRelational(obj, tbl, row);

               adapter.Update(tbl);

               if (originalObjectState == ObjectState.Deleted)
                   obj = null;
               else RelationalToObject(obj, tbl.Rows[0]);
           }
       }

       #endregion

       #region Validate

       /// <summary>
       /// Validates the object based on its properties at the time of the call; updates the Errors property.
       /// </summary>
       public override bool Validate()
       {
           Errors.Clear();

           if (ObjState == ObjectState.Unchanged)
           {
               // By definition, unchanged objects are valid.
               return (true);
           }
           else if (ObjState == ObjectState.Uninitialized)
           {
               Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

               // No need to proceed past this error.
               return (false);
           }
           else if ((ObjState == ObjectState.Deleted))
           {
               // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
               Errors.Add(new ValidationError("UserFeedbackId", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

               // No need to proceed past this error.
               return (false);
           }
           else  // ObjState == Added | Modified
           {

           }

           return (Errors.Count == 0);
       }

       #endregion

       #region ToString

       /// <summary>
       /// User-friendly string represetation of this object.
       /// </summary>
       public override string ToString()
       {
           return (String.Format("UserFeedback: UserFeedbackId={0}, FirstName={1}, {2}", UserFeedbackId, FirstName,LastName));
       }

       #endregion

       #endregion
    }

   /// <summary>
   /// Collection of 0 or more SubAdmin objects.
   /// </summary>

   public class UserFeedbackCollection : List<UserFeedback>
   {

       #region Types

       public class UserComparer : IComparer<UserFeedback>
       {
           public int Compare(UserFeedback user1, UserFeedback user2)
           {
               return (String.Compare(user1.FirstName, user2.FirstName));
           }
       }

       #endregion

       #region Public Methods

       #region FindByUserFeedbackID

       public UserFeedback FindByUserFeedbackID(int userfeedbackID)
       {
           for (int ndx = 0; ndx < this.Count; ndx++)
               if (this[ndx].UserFeedbackId == userfeedbackID)
                   return (this[ndx]);

           return (null);
       }

       #endregion

       #region FindByEmailAddress

       /// <summary>
       /// Case insensitve compare.
       /// </summary>
       public UserFeedback FindByEmailAddress(string emailaddress)
       {
           for (int ndx = 0; ndx < this.Count; ndx++)
               if (this[ndx].EmailAddress == emailaddress)
                   return (this[ndx]);

           return (null);
       }

       #endregion

       #endregion

   }
}
